// pages/address/list/index.js
const app = getApp();
import {getAddressList,removeAddress} from '../../../api/address'
Page({
  // 页面的初始数据
  data: {
  },

  // 去编辑页面
  toEdit(e) {
    const {data}=e.currentTarget.dataset
    const params=data[0]
    const id=data[1]
    app.editaddressitem=params
    app.addressid=id
    app.onedit=true
    wx.navigateTo({
      url: '/modules/settingmodule/pages/address/add/index'
    })
  },
  async getlist(){
     const res= await getAddressList()
     this.setData({
       addressList:res.data
     })
  },
  async remove(e){
    const {id}=e.currentTarget.dataset
    await removeAddress(id)
    wx.showToast({
      title: '删除成功',
    })
    // 重新加载数据
    this.getlist()
  },
  addresstoapp(e){
    const {item}=e.currentTarget.dataset
    if(app.isorderall==true){
      app.temptaddress=item
    }
    wx.navigateTo({
      url: `/modules/orderpaymodule/pages/order/detail/detail?iseditback=true`,
    })
  },
  async onShow(){
  // 每次页面展示都加载数据
   this.getlist()
  },
  onLoad(options){
   const {isorder,isorderall}= options
   if(isorderall=="true"){
     app.isorderall=true
   }
   if(isorder=="true"){
     app.flag=true
   }
  },
  onUnload(){
    app.isorderall=false
  }
})
